const app = getApp();
const common = require('../../utils/common.js');
const api = require('../../config/api.js');

var pageNo = 1;
var hadLastPage = false;
var isLoad = 0;

Page({
  data: {
		imgUrl: app.globalData.imgurl,
		dataList: [],
		showNoData: false,
		showLoding: true,
  },
	onShow: function () {
		this.clearData();
		this.loadList();
	},
	clearData: function () {
		pageNo = 1;
		hadLastPage = false;
		this.setData({
			dataList: []
		});
	},
	loadList: function () {
		isLoad = 1;
		if (hadLastPage) {
			isLoad = 0;
			return;
		}
		var that = this;
		var data = {
			pageNo: pageNo
		}
		this.setData({
			showLoding: true,
			showNoData: false
		});
		setTimeout(function () {
			common.request(api.OrderStaffTiHuoRecord, data, 'POST').then(function (data) {
				that.setData({
					showLoding: false
				});
				if (data.type == 'success') {
					var dataList = that.data.dataList;
					for (var i = 0; i < data.data.dataList.length; i++) {
						dataList.push(data.data.dataList[i]);
					}
					if (data.data.pageNo == data.data.totalPages) {
						hadLastPage = true;
					} else {
						pageNo++;
					}
					that.setData({
						dataList: dataList
					});
					isLoad = 0;
					if (data.data.total == 0) {
						that.setData({
							showNoData: true
						});
					}
				} else {
					console.log(data.content);
					isLoad = 0;
				}
			});
		}, app.globalData.showLodingTime);
	},
	onReachBottom: function () {
		this.loadList();
	},
  saoma: function () {
    wx.scanCode({
      onlyFromCamera: false,
      scanType: ['barCode', 'qrCode', 'datamatrix', 'pdf417'],
      success: res => {
        if (res.errMsg == 'scanCode:ok' && res.scanType == 'QR_CODE') {
          var content = res.result;
					if (content.indexOf('type=staff&oulehui_order_id=') >= 0) {
						content = content.replace('oulehui_order_id', 'orderId');
						wx.navigateTo({
							url: '/pages/ucenter/orderDetail/orderDetail?' + content
						})
					} else {
						common.showErrorToast('请扫描正确的二维码');
					}
        } else {
					common.showErrorToast('请扫描正确的二维码');
				}
      },
      fail: res => {
				common.showErrorToast('请扫描正确的二维码');
      },
      complete: res => {
        console.log(res)
      }
    });
  }
})